# -*- coding: utf-8 -*-
import pymysql
import re
import func_get_author
import func_get_info

# 打开数据库连接
connection = pymysql.connect(host = '',
                            user = '',
                            password = '',
                            db = '',
                            charset = 'utf8')

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = connection.cursor()
 
# 使用 execute()  方法执行 SQL 查询 
cursor.execute("SELECT * from lunwen")
 
# 使用 fetchall() 方法获取多条数据.
datas = cursor.fetchall()
for data in datas:
    flag  = data[0]
    # 论文标题，用于构建作者之间的联系
    title = data[2]
    # 论文作者名单
    author = data[3].strip('\n')
    # 论文作者的信息
    author_info = data[4].strip('\n')
    # 算作论文作者的兴趣，也就是关键词
    guanjiang = data[6].replace('　','').replace(' ','').replace(':','').replace('：','')

    # 通过对论文的作者进行处理，得到作者名称和编号字典
    author_dict = func_get_author.get_author_and_number(author)
    # 通过对论文的作者信息进行处理，得到编号和单位字典,编号和职称字典,编号和邮编字典,编号和地址字典
    zhicheng_dict,school_dict,address_dict,code_dict = func_get_info.get_info(author_info)

    insert_list = []
    for key,value in author_dict.items():
        insert_list.append((title,key,zhicheng_dict[value],school_dict[value],address_dict[value],guanjiang,code_dict[value]))

    # print(insert_list)
    author_sql = "insert into author_info (a_title,a_name,a_zhicheng,a_dangwei,a_dizhi,a_xingqu,a_youbian) values (%s,%s,%s,%s,%s,%s,%s)"
    
    cursor.executemany(author_sql,insert_list)
    print(flag)

    #提交数据库
    connection.commit()

# 关闭数据库连接
connection.close()
print('over')